import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 * Login GUI
 * @author Jeroen
 *
 */
public class LoginGUI extends JFrame implements ActionListener,KeyListener{

	private static final long serialVersionUID = 1L;
	private int pWidth = 600, pHeight = 600;
	private Bank bank;
	
	private JLabel message, message2, message3, lege, lege2;
	private JTextField username;
	private JPasswordField wachtwoord;
	private JButton confirm;
	private JPanel jp1;
	
	public LoginGUI(Bank b){

		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		jp1 = new JPanel();
		jp1.setLayout(new GridLayout(4,2));
		c.add(jp1);
		
		bank = b;
		
		username = new JTextField("", 10);
		message = new JLabel("Voer gebruikersnaam in:");
		message2 = new JLabel("Voer wachtwoord in:");
		message3 = new JLabel(" ");
		message3.setForeground(Color.RED);
		wachtwoord = new JPasswordField("", 10);
	    confirm = new JButton("Inloggen");
		lege = new JLabel(" ");
		lege2 = new JLabel(" ");
		
		jp1.add(message3);
		jp1.add(lege2);
		jp1.add(message);
		jp1.add(username);
		jp1.add(message2);
		jp1.add(wachtwoord);
		jp1.add(lege);
		jp1.add(confirm);
		confirm.addActionListener(this);
		wachtwoord.addKeyListener(this);
		username.addKeyListener(this);
		
		setSize(pWidth, pHeight);
	    setVisible(true);
	    setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public void keyTyped(KeyEvent e){}
	public void keyPressed(KeyEvent e){}
	
	public void keyReleased(KeyEvent e){
		if(e.getKeyCode() == 10){
			HashMap klanten = bank.klanten();
			String username = this.username.getText();
			Klant klant = (Klant) klanten.get(username);
			char[] input = wachtwoord.getPassword();
			String s = new String( input );
			if (klant != null && klant.checkWachtwoord(s)){
				new SelectRekeningGUI(this.bank, klant);
				dispose();
			}else if (klant == null){
				HashMap medewerkers = bank.medewerkers();
				BankMedewerker medewerker = (BankMedewerker) medewerkers.get(username);
				if (medewerker != null && medewerker.checkWachtwoord(s)){
					new MenuGUI(bank);
					dispose();
				}
				else{
					this.message3.setText("Foutieve inlognaam");
				}
			}else{
				this.message3.setText("Fout wachtwoord!");
			}
			
		}	
	}
	
	
	
	public void actionPerformed(ActionEvent arg0) {
		HashMap klanten = bank.klanten();
		String username = this.username.getText();
		Klant klant = (Klant) klanten.get(username);
		char[] input = wachtwoord.getPassword();
		String s = new String( input );
		if (klant != null && klant.checkWachtwoord(s)){
			new SelectRekeningGUI(this.bank, klant);
			dispose();
		}else if (klant == null){
			HashMap medewerkers = bank.medewerkers();
			BankMedewerker medewerker = (BankMedewerker) medewerkers.get(username);
			if (medewerker != null && medewerker.checkWachtwoord(s)){
				new MenuGUI(bank);
				dispose();
			}
			else{
				this.message3.setText("Foutieve inlognaam");
			}
		}else{
			this.message3.setText("Fout wachtwoord!");
		}
		
	}

}
